import time
from selenium.webdriver.support.wait import WebDriverWait

from config import PATH


class AppBasePage:

    def __init__(self, driver):
        self.driver = driver

    def app_fd_element(self, loc):
        """
        封装公共元素定位方法
        :param loc: 元素定位方式及属性值
        :return: 元素
        """
        try:
            ele = WebDriverWait(self.driver, 20).until(lambda x: x.find_element(*loc))
            return ele
        except Exception as e:
            print("元素定位失败：{}".format(e))
            raise

    def app_base_click(self, loc):
        """
        封装点击方法
        :param loc: 元素定位方式及属性值
        :return:
        """
        self.app_fd_element(loc).click()

    def app_base_input(self, loc, text):
        """
        封装输入方法
        :param loc: 元素定位方式及属性值
        :param text: 输入内容
        :return:
        """
        self.app_fd_element(loc).clear()  # 清除默认内容
        self.app_fd_element(loc).send_keys(text)  # 输入内容
        time.sleep(1)

    def app_shot_screen(self, file_name):
        """
        截图
        :param file_name: 文件名
        :return:
        """
        self.driver.get_screenshot_as_file(PATH+'/img/'+file_name+'.png')